package com.psp.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.northend.util.NumUtil;
import com.psp.web.controller.res.BaseResult;
import com.psp.web.controller.res.ListResult;
import com.psp.web.controller.res.bean.RExcMsgListBean;
import com.psp.web.controller.res.bean.RMsgListBean;
import com.psp.web.controller.res.bean.RSysMsgListBean;
import com.psp.web.controller.springmvc.req.GetDelMsgParam;
import com.psp.web.controller.springmvc.req.GetExcMsgParam;
import com.psp.web.controller.springmvc.req.GetMsgAllParam;
import com.psp.web.controller.springmvc.req.GetSysMsgParam;
import com.psp.web.model.UserMsgBean;
import com.psp.web.service.MsgService;
import com.psp.web.service.exception.ServiceException;
import com.psp.web.service.impl.PageResult;

@Component
public class MsgController {

	@Autowired
	MsgService msgServiceImpl;

	/**
	 * 获取全部消息
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RMsgListBean> getAll(GetMsgAllParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RMsgListBean> result = new ListResult<>();
		List<RMsgListBean> resList = new ArrayList<>();

		int page = NumUtil.toInt(param.getPage(), 0);
		int pageSize = 20;
		String uid = (String) request.getAttribute("userId");
		Integer msgType = null; // 消息类型 1交易消息 2系统消息 为空表示全部
		Integer type = null; // 消息类型：1用户发送给业务组 2业务组发送给用户
		PageResult<UserMsgBean> resData = msgServiceImpl.getMsgs(page, pageSize, uid, type, msgType);
		if (resData.getData() != null) {
			for (UserMsgBean bean : resData.getData()) {
				RMsgListBean rb = new RMsgListBean();
				rb.setId(String.valueOf(bean.getId()));
				rb.setOrderNo(bean.getOrderNo());
				rb.setTitle(bean.getTid());
				rb.setMsgDate(bean.getMsgDate() != null ? bean.getMsgDate().getTime() : 0);
				rb.setContent(bean.getContent());
				resList.add(rb);
			}
		}

		result.setData(resList);
		result.setTotalSize(resData.getCount());
		result.setPage(page);
		return result;
	}

	/**
	 * 获取交易消息
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RExcMsgListBean> getExc(GetExcMsgParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RExcMsgListBean> result = new ListResult<>();
		List<RExcMsgListBean> resList = new ArrayList<>();

		int page = NumUtil.toInt(param.getPage(), 0);
		int pageSize = 20;
		String uid = (String) request.getAttribute("userId");
		Integer msgType = 1; // 消息类型 1交易消息 2系统消息 为空表示全部
		Integer type = null; // 消息类型：1用户发送给业务组 2业务组发送给用户
		PageResult<UserMsgBean> resData = msgServiceImpl.getMsgs(page, pageSize, uid, type, msgType);
		if (resData.getData() != null) {
			for (UserMsgBean bean : resData.getData()) {
				RExcMsgListBean rb = new RExcMsgListBean();
				rb.setId(String.valueOf(bean.getId()));
				rb.setOrderNo(bean.getOrderNo());
				rb.setTitle(bean.getTid());
				rb.setMsgDate(bean.getMsgDate() != null ? bean.getMsgDate().getTime() : 0);
				rb.setContent(bean.getContent());
				resList.add(rb);
			}
		}

		result.setData(resList);
		result.setTotalSize(resData.getCount());
		result.setPage(page);
		return result;
	}

	/**
	 * 获取系统消息
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RSysMsgListBean> getSys(GetSysMsgParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RSysMsgListBean> result = new ListResult<>();
		List<RSysMsgListBean> resList = new ArrayList<>();

		int page = NumUtil.toInt(param.getPage(), 0);
		int pageSize = 20;
		String uid = (String) request.getAttribute("userId");
		Integer msgType = 2; // 消息类型 1交易消息 2系统消息 为空表示全部
		Integer type = null; // 消息类型：1用户发送给业务组 2业务组发送给用户
		PageResult<UserMsgBean> resData = msgServiceImpl.getMsgs(page, pageSize, uid, type, msgType);
		if (resData.getData() != null) {
			for (UserMsgBean bean : resData.getData()) {
				RSysMsgListBean rb = new RSysMsgListBean();
				rb.setId(String.valueOf(bean.getId()));
				rb.setTitle(bean.getTid());
				rb.setMsgDate(bean.getMsgDate() != null ? bean.getMsgDate().getTime() : 0);
				rb.setContent(bean.getContent());
				resList.add(rb);
			}
		}

		result.setData(resList);
		result.setTotalSize(resData.getCount());
		result.setPage(page);
		return result;
	}

	public BaseResult del(GetDelMsgParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			String uid = (String) request.getAttribute("userId");
			Long id = param.getId();
			boolean flag = msgServiceImpl.del(uid, id);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

}
